package org.exercise10.state.impl;

import org.exercise10.state.Context;
import org.exercise10.state.DoorStatus;

/**
 * @author Stranger
 * @version 1.0
 * @description: TODO
 * @date 2022/12/20 17:32
 */
public class OpenedState extends DoorStatus {

    @Override
    public void open() {
        System.out.println("门已经打开");
        new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            this.context.setState(Context.STAY_OPEN_STATE);
            this.context.stayOpen();
        }).start();
        try {
            Thread.sleep(3000);
            System.out.println("时间超时");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            if (this.context.getState().toString().equals(Context.OPENED_STATE.toString()) || this.context.getState().toString().equals(Context.STAY_OPEN_STATE.toString())) {
                this.context.close();
            }
        }

    }

    @Override
    public void close() {
        this.context.setState(Context.STAY_OPEN_STATE);
        this.context.close();
    }

    @Override
    public void touch() {
        this.context.setState(Context.STAY_OPEN_STATE);
        this.context.stayOpen();
    }

    @Override
    public String toString() {
        return "门已打开";
    }
}
